package com.ts.movie.controller;

import com.ts.movie.dto.MovieDTO;
import com.ts.movie.dto.ScoreDto;
import com.ts.movie.entity.Movie;
import com.ts.movie.service.MovieScoreService;
import com.ts.movie.service.MovieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ts.common.entity.ResponseResult;
import com.ts.common.security.annotation.LoginUser;
import com.ts.common.security.dto.UserToken;

import java.util.List;

@RestController
@RequestMapping("/movie")
@Slf4j
public class MovieController {

    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieScoreService movieScoreService;

    @GetMapping("/action/query")
    public ResponseResult query() {
        List<Movie> list = movieService.list();
        return ResponseResult.success(list);
    }

    @GetMapping("/{id}")
    public ResponseResult query(@PathVariable Integer id) {
        Movie movie = movieService.getById(id);
        return ResponseResult.success(movie);
    }

    @PostMapping
    public ResponseResult add(@RequestBody MovieDTO movie) {
        log.debug("接收到的:{}" + movie);
        movieService.save(movie);
        return ResponseResult.success();
    }


    @PostMapping("/action/update")
    public ResponseResult update(@RequestBody MovieDTO movie) {
        movieService.updateById(movie);
        return ResponseResult.success();
    }

    @PostMapping("/action/delete")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        movieService.removeBatchByIds(ids);
        return ResponseResult.success();
    }

    @PostMapping("/score")
    public ResponseResult score(@LoginUser UserToken userToken, @RequestBody ScoreDto scoreDto) {
        movieScoreService.score(userToken, scoreDto);
        return ResponseResult.success();
    }
}
